﻿Public Class frmHolidayDE
    Dim validator As PropertyValidator = New PropertyValidator
    Dim payrollManger As New PayrollManager

    Private Sub frmHolidayDE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ClearAllFields()
        If Clipboard.GetData(DataFormats.Text) <> "" Then
            Try
                Dim holList As List(Of Holiday) = payrollManger.getItemHoliday(Clipboard.GetData(DataFormats.Text))
                For Each hol As Holiday In holList
                    txtDescription.Text = hol.description
                    dtpDate.Text = hol.holiday_date
                    cbType.Text = If((hol.type = 1), "Legal", "Special")
                Next
                btnSave.Text = "&Update"
                btnClear.Text = "&Cancel"
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If validator.IsEnteredInTheComboBoxList(cbType, "Holiday Type") Then Return
        Try
            Dim holidayObject As New Holiday
            holidayObject.description = txtDescription.Text
            holidayObject.holiday_date = dtpDate.Text
            holidayObject.type = If((cbType.Text = "Legal"), "1", "0")
            holidayObject.id = Clipboard.GetData(DataFormats.Text)
            If Clipboard.GetData(DataFormats.Text) = "" Then
                'sqlManager.executeQuery("INSERT INTO tbl_philhealth (bracket,min_salary, max_salary, base_salary, employer_share, employee_share) VALUE (" + txtBraket.Text + "," + txtMinimumSalary.Text + "," + txtMaximumSalary.Text + "," + txtSalaryBase.Text + "," + txtEmployerShare.Text + "," + txtEmployeeShare.Text + ")")


                payrollManger.insertHoliday(holidayObject)
                MsgBox("Successfully saved.")
            Else
                'sqlManager.executeQuery("UPDATE tbl_philhealth SET bracket=" + txtBraket.Text + ",min_salary = " + txtMinimumSalary.Text + ", max_salary = " + txtMaximumSalary.Text + ", base_salary =" + txtSalaryBase.Text + " , employer_share =" + txtEmployerShare.Text + " , employee_share = " + txtEmployeeShare.Text + " WHERE id = " + Clipboard.GetData(DataFormats.Text) + "")
                payrollManger.updateItemHoliday(holidayObject)
                MsgBox("Successfully updated.")
            End If
            ClearAllFields()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
        Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        If Clipboard.GetData(DataFormats.Text) = "" Then
            validator.ResetControls(grpHoliday)
        Else
            Me.Close()
        End If
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpHoliday)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function
End Class